
;;
;; reimplement some kernel functions for kernel dll
;;

    OPT     2       ; disable listing
    INCLUDE ksarm.h
    OPT     1       ; reenable listing

	IMPORT pKData
	IMPORT pNKSection

    TEXTAREA



        LEAF_ENTRY _SetCPUASID

;;; (r12) = ptr to KDataStruct
        ldr     r12, =pKData
        ldr     r12, [r12]
        ldr     r1, [r0, #ThProc]               ; (r1) = ptr to thread's current process
        ldr     r2, [r12, #CeLogStatus]         ; (r2) = KInfoTable[KINX_CELOGSTATUS]

        ; (r1) = pTh->pProc
        ; (r2) = KInfoTable[KINX_CELOGSTATUS]
        ; (r12) = ptr to KData
        ;
;;;        ldr     r0, =CELOGSTATUS_ENABLED_GENERAL ; (r0) = status flag
;;;        tst     r2, r0                          ; is the status flag set in r2?
        ldr     r0, [r1, #PrcHandle]            ; (r0) = handle of new process
;;; no log for CeLogThreadMigrate
;;;        bne     %F35                            ; branch if IsCeLogStatus(CELOGSTATUS_ENABLED_GENERAL)
31
        ;   (r0) = pTh->pProc->hProc (handle of new process)
        ;   (r1) = pTh->pProc
        ;   (r12) = ptr to KData
        ;
        ldr     r3, [r1, #PrcVMBase]            ; (r3) = current proc's slot base address
        str     r1, [r12, #pCurPrc]             ; set pCurProc
        str     r0, [r12, #hCurProc]            ; set hCurProc
        add     r12, r12, #aSections            ; (r12) = ptr to SectionTable

        ldrb    r0, [r1, #PrcID]                ; (r0) = pCurProc->procnum
        cmp     r0, #0                          ; is it NK?
;;; use NK Section if it is
        ldreq   r2, =pNKSection
        ldreq   r2, [r2]
        ldrne   r2, [r12, r3, LSR #VA_SECTION-2]  ; use per-process's Section if not,
                                                ; (r2) = process's memory section
                                                
;        mtc15   r3, c13                         ; set process base address register
        mcr     p15, 0, r3, c13, c0, 0
        str     r2, [r12]                       ; copy to slot 0 for VirtualAlloc & friends
        ; Set DOMAIN Access.

;;;        RETURN                                  ; return to caller
        mov     pc, lr

    END

